﻿import com.dmotiko.general.*
import com.dmotiko.util.MovieClipUtils;
import com.dmotiko.util.Point
import mx.data.encoders.Bool;
import mx.utils.Delegate

class SelectorClip
extends BaseClip {
	
	private var input:SelectorInput;
	private var list:SelectorList;
	private var sInputLinkage:String;
	private var sListLinkage:String;
	private var selectedItem:Object;
	private var oMouseListener:Object;
	private var label:String;
		
	function SelectorClip() {
		super();
	}
	
	private function initClip() {
		this.sInputLinkage = "SelectorInput";
		this.sListLinkage = "SelectorList";
		
		label = "seleccione opcion";
		
		if( !input ) input = SelectorInput(this.attachMovie( sInputLinkage, "input", this.getNextHighestDepth()));
				
		input.addEventListener( BaseClipEvent.CHANGED, Delegate.create( this, inputChanged));
		input.addEventListener( SelectorInput.SET_FOCUS, Delegate.create( this, inputChanged));
		input.addEventListener( SelectorInput.KILL_FOCUS, Delegate.create( this, inputChanged));
		
		oMouseListener = new Object();
		oMouseListener.input = input;
		oMouseListener.onMouseDown = function() {
			if ( !this.input.hitTest(_root._xmouse, _root._ymouse) && !this.list.hitTest( _root._xmouse, _root._ymouse ) ) {
				Selection.setFocus(null);
				list.hide();
			}
			
		}
	}
	
	public function setLabel(sLabel:String):Void {
		label = sLabel;
		this.input.setText(label);
	}
	
	public function childLoaded( evnt ):Void {
		switch(evnt.target) {
			case input:
			input.setText(label);
			break;
		}
	}
	
	private function inputChanged( evnt ) {
		switch( evnt.type ) {
			case BaseClipEvent.CHANGED:
			list.drawList( input.getText().toLowerCase());
			break;
			
			case SelectorInput.KILL_FOCUS:
			Mouse.removeListener( oMouseListener );
			if ( input.getText().length < 1 ) input.setText( label );
			break;
			
			case SelectorInput.SET_FOCUS:
			if ( !list ) drawList();
			Mouse.addListener( oMouseListener );
			if ( ! list.getData() ) list.setData( this.data );
			input.setText("");
			input.dispatchEvent( { type: BaseClipEvent.CHANGED, target: input } );
			break;
		}
	}
	
	private function drawList() {
		var nDepth:Number = _root.getNextHighestDepth();
		if ( _root.LIST_CLIP_DEPTH ) nDepth = _root.LIST_CLIP_DEPTH;
		list = SelectorList( _root.attachMovie( sListLinkage, "SELECTOR_LIST",  nDepth));
		_root.LIST_CLIP_DEPTH = list.getDepth();
				
		oMouseListener.list = list;
		var p = { x: 0, y: 0 };
		input.localToGlobal( p );
		list._x = p.x;
		list._y = p.y + input._height + 5;
		list.setSize( new Point( list._width, 100 ) );
		list.addEventListener( BaseClipEvent.CHANGED, Delegate.create( this, listChanged) );
	}
	
	private function listChanged( evnt ) {
		if( evnt.selectedItem ){
			list.hide();
			input.setText(evnt.selectedItem.key);
			Selection.setFocus(null);
			this.selectedItem = evnt.selectedItem;
			dispatchEvent( { type: BaseClipEvent.CHANGED, target: this, data: selectedItem } );
		}
	}
		
	public function setSelectedItem( item, noEvent:Boolean ):Void {
		for (var i:Number = 0; i < data.length; i++) {
			if (data[i] == item) {
				input.setText(item.key);
				Selection.setFocus(null);
				this.selectedItem = item;
				if(!noEvent) dispatchEvent( { type: BaseClipEvent.CHANGED, target: this, data: item } );
			}
		}
	}
	
	public function getSelectedItem():Object {
		return this.selectedItem;
	}
	
}